package com.demohomework.homeworkdemo.service.impl;

import com.demohomework.homeworkdemo.mapper.AdminMapper;
import com.demohomework.homeworkdemo.pojo.Admin;
import com.demohomework.homeworkdemo.service.AdminService;
import com.demohomework.homeworkdemo.util.Md5Util;

import com.demohomework.homeworkdemo.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired

    private AdminMapper adminMapper;
    @Override
    public Admin findbyUsername(String username) {
        Admin u = adminMapper.findbyUsername(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5Password  = Md5Util.string2MD5(password);
        //添加
        adminMapper.add(username, md5Password);
    }

    @Override
    public void update(Admin admin) {
         adminMapper.update(admin);
    }

    @Override
    public void updatePwd(String newPwd) {
        //加密
        String md5Password  = Md5Util.string2MD5(newPwd);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        //修改
        adminMapper.updatePwd(md5Password,id);
    }

    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }

    @Override
    public void delete(Integer id) {
         adminMapper.delete(id);
    }

    @Override
    public void add(Admin admin) {
        String defaultPassword = "123456";
        String md5Password  = Md5Util.string2MD5(defaultPassword);
        admin.setPassword(md5Password);
        adminMapper.adminAdd(admin);
    }


}
